package br.com.winesys.geta.databinding.componentBinder;

import br.com.winesys.geta.databinding.ComponentBinder;
import br.com.winesys.geta.databinding.component.editor.CharacterTextField;

public final class CharacterTextFieldBinder implements ComponentBinder<CharacterTextField> {

	private static final ComponentBinder<CharacterTextField> INSTANCE = new CharacterTextFieldBinder();

	public static ComponentBinder<CharacterTextField> getInstance() {
		return INSTANCE;
	}

	private CharacterTextFieldBinder() {
	}

	@Override
	public Object read(CharacterTextField component) {
		return component.getText().isEmpty() ? null : new Character(component.getText().charAt(0));
	}

	@Override
	public void write(CharacterTextField component, Object value) {
		if (value == null) {
			component.setText(null);
		} else {
			component.setText(value.toString());
		}
		component.setCaretPosition(0);
	}
}
